package com.ysq.ip.spider.config;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import com.ysq.ip.spider.util.CommonUtil;

/**
 * 加载 base config
 * @author admin
 *
 */
public final class BaseConfigInfo {
	static Logger logger = CommonUtil.getLogger(BaseConfigInfo.class);
	
	/**************************************** 配置参数 ****************************************/
	/** 项目主路径 **/
	public static String projectRootDir;
	
	/** 配置文件路径 **/
	static String configFilePath = "config/config.xml";
	
	/** ========== config ========== **/
	public static List<String> area_list;
	
	public static List<String> url_list;
	
	static{
		projectRootDir = System.getProperty("user.dir");
		configFilePath = projectRootDir + File.separator + configFilePath;
		
		try {
			initAllConfig(configFilePath);
		} catch(Exception e) {
			logger.error("load config file error ... ", e);
		}
	}
	
	/**
	 * 初始化所有config
	 * @param configfile
	 * @throws ConfigurationException
	 */
	public static void initAllConfig(String configFilePath) throws Exception {
		String xmlContent = CommonUtil.getFileContent(configFilePath);
		
		Document doc = Jsoup.parse(xmlContent, "", Parser.xmlParser());
		
		loadConfig(doc);
	}
	
	private static void loadConfig(Element rootEle) {
		String areaListStr = CommonUtil.selectElementText(rootEle, "config>area_list");
		String[] areaArray = areaListStr.split(",");
		
		area_list = Arrays.asList(areaArray);
		
		String urlListStr = CommonUtil.selectElementText(rootEle, "config>url_list");
		String[] urlArray = urlListStr.split(",");
		
		url_list = Arrays.asList(urlArray);
		
		logger.info("load config success ... ");
	}
	
	public static void main(String[] args) {
		
		System.out.println("area_list : " + area_list);
		
		System.out.println("success passed ... ");
		
		
	}

}
